﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Media;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Tank.Draw;

namespace Tank
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            //激活双缓冲技术
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.DoubleBuffer, true);
        }

        bool isOk = false;
        private void MainForm_Paint(object sender, PaintEventArgs e)
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();
            isOk = false;
            Graphics g = e.Graphics;
            g.Clear(Color.Black);
            switch (Data.indexState)
            {
                case Data.GameState.StartFrame:
                    DrawStart gdi = new DrawStart(ClientSize.Width, ClientSize.Height);
                    g.DrawImage(gdi.getBitmap(), 0f, 0f);
                    break;
                case Data.GameState.GameFrame:
                    break;
            }
            isOk = true;
            sw.Stop();
            TimeSpan ts2 = sw.Elapsed;
            label1.Text = "FPS:" + (1000 / ts2.TotalMilliseconds).ToString("0");
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            Thread t = new Thread(startGame);
            t.IsBackground = true;
            t.Start();
            music();
        }

        private void startGame()
        {
            while (true)
            {
                try
                {
                    if (isOk)
                        this.Invalidate();
                    else
                        Thread.Sleep(1);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
            }
        }

        private void MainForm_KeyDown(object sender, KeyEventArgs e)
        {
            switch (Data.indexState)
            {
                case Data.GameState.StartFrame:
                    switch (e.KeyCode)
                    {
                        case Keys.Up:
                        case Keys.W:
                            Data.indexselectTankState = Data.getLastSelectTankState();
                            break;
                        case Keys.Down:
                        case Keys.S:
                            Data.indexselectTankState = Data.getNextSelectTankState();
                            break;
                        case Keys.Enter:
                            Data.indexState = Data.GameState.GameFrame;
                            break;
                    }
                    selectMusic();
                    break;
                case Data.GameState.GameFrame:
                    GameKeyDown(e);
                    break;
            }
        }

        private void GameKeyDown(KeyEventArgs e)
        {

        }

        private void music()
        {
            SoundPlayer sp = new System.Media.SoundPlayer(AppDomain.CurrentDomain.BaseDirectory + @"\Music\start.wav");
            sp.PlayLooping();
        }

        private void selectMusic()
        {
            SoundPlayer sp = new System.Media.SoundPlayer(AppDomain.CurrentDomain.BaseDirectory + @"\Music\fire.wav");
            sp.Play();
        }
    }
}
